/**
 *	登录后主页控制器
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Controller.api;


import com.framerock.server.Response;
import com.framerock.baselib.CDate;
import com.framerock.baselib.Json;
import com.framerock.baselib.Patter;
import com.framerock.baselib.Rock;
import com.framework.Chajian.Chajianrockfile;
import com.framework.ACore.Model;
import com.framework.Model.ModelCompany;
import com.framework.Model.ModelOption;

import java.util.Map;


public class ControllerFile extends ControllerApi {




    protected String runAction(String action)
    {
        if(action.equals("save"))return save();
        if(action.equals("del"))return del();
        return super.runAction(action);
    }

    //保存上传文件
    public String save()
    {
        Model obj       = Model.get(this.nDb, "file");
        String filenum  = mRequest.post("filenum");
        String fileid   = mRequest.post("id");
        if(Rock.isEmpt(filenum) || !Patter.iszgen(filenum) || Rock.isEmpt(fileid))return Response.returnError("params is empty");

        Map<String, String> da = Rock.getMap();
        String where = "`filenum`='"+filenum+"'";
        String url = new Chajianrockfile(this.nDb).geturl();

        da.put("filenum", filenum);
        da.put("fileid", fileid);
        da.put("filename", mRequest.post("filename"));
        da.put("filesize", mRequest.post("filesize"));
        da.put("filesizecn", mRequest.post("filesizecn"));
        da.put("fileext", mRequest.post("fileext"));

        String filepath         = mRequest.post("filepath");
        String thumbpath        = mRequest.post("thumbpath");
        String filepathout      = mRequest.post("filepathout");
        String thumbpathout     = mRequest.post("thumbpathout");

        if(!Rock.isEmpt(filepathout))filepath   = filepathout;
        if(!Rock.isEmpt(thumbpathout))thumbpath = thumbpathout;
        if(!Rock.isEmpt(filepath) && !Rock.contain(filepath, "uptemp") && filepath.indexOf("http")==-1)filepath = url+filepath;
        if(!Rock.isEmpt(thumbpath) && thumbpath.indexOf("http")==-1)thumbpath   = url+thumbpath;

        da.put("filepath", filepath);
        da.put("thumbpath", thumbpath);

        String optname = mRequest.post("optname");
        if(mRequest.COMPANYBOOL) {
            da.put("cid", mRequest.COMPANYID);
            da.put("aid", mRequest.USEAID);
            if(mRequest.USERABOOL){
                optname = mRequest.getUserars("name");
            }
        }
        da.put("optname", optname);
        int id = 0;
        if(obj.getCount(where)==0){
            da.put("adddt", CDate.now());
            id = obj.insert(da);
        }else{
            obj.update(da, where);
            id = Rock.getMapint(da, "id");
        }
        Json json = new Json();
        json.putInt("id", id);
        json.putVal("filenum", filenum);
        return Response.returnSuccess(json.getJson());
    }

    //获取文件信息
    public String data()
    {
        String fnum = mRequest.get("fnum");
        String lx   = mRequest.get("lx");
        if(Rock.isEmpt(fnum) || !Patter.iszgen(fnum))return Response.returnError("fnum params error");
        if(Rock.isEmpt(lx) || !Patter.isnumber(lx))return Response.returnError("lx params error");
        String cont = Chajianrockfile.obj(this.nDb).info(fnum, lx);
        return cont;
    }

    //删除文件
    public String del()
    {
        String fnum = mRequest.get("fnum");
        if(Rock.isEmpt(fnum) || !Patter.iszgen(fnum))return Response.returnError("fnum params error");
        Model.get(this.nDb, "file").delete("`filenum`='"+fnum+"'");
        Chajianrockfile.obj(this.nDb).del(fnum);
        return Response.returnSuccess();
    }
}
